<html>
<head>
<link rel='stylesheet' href='../../js/sh/SyntaxHighlighter.css' type='text/css' />
<script src='../../js/sh/shCore.js'></script>
<script src='../../js/sh/shBrushJava.js'></script>
<style>
* {
font-family:Courier New,monospace;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  font-size: 11px;
}
.dp-highlighter {
  white-space: nowrap;
  overflow: visible;
  width: 600px;
  font-size: 11px;
  font-family:Courier New,monospace;
}
</style>
</head>
<body>
<textarea name='code' class='java:nogutter' rows='15' cols='120'>
/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.sample.showcase.client.data.EmployeeXmlDS;
import com.smartgwt.sample.showcase.client.data.TeamMembersXmlDS;

public class GridDataBoundDragDropSample implements EntryPoint {

    @Override
    protected boolean isTopIntro() {
        return true;
    }

    public void onModuleLoad() {

        String[] projects = new String[] {
                "New Costing System", "Warehousing Improvements", "Evaluate AJAX Frameworks", "Upgrade Postgres", "Online Billing"
        };

        DataSource employeesDS = EmployeeXmlDS.getInstance();

        final ListGrid employeesGrid = new ListGrid();
        employeesGrid.setWidth(300);
        employeesGrid.setHeight(224);
        employeesGrid.setDataSource(employeesDS);
        employeesGrid.setCanDragRecordsOut(true);
        employeesGrid.setDragDataAction(DragDataAction.COPY);
        employeesGrid.setAlternateRecordStyles(true);
        employeesGrid.setAutoFetchData(false);

        ListGridField employeeIdField = new ListGridField("EmployeeId");
        employeeIdField.setWidth("25%");

        ListGridField nameField = new ListGridField("Name");

        employeesGrid.setFields(employeeIdField, nameField);


        DataSource teamMembersDS = TeamMembersXmlDS.getInstance();

        final ListGrid projectGrid = new ListGrid();
        projectGrid.setWidth(300);
        projectGrid.setHeight(264);
        projectGrid.setDataSource(teamMembersDS);
        projectGrid.setCanAcceptDroppedRecords(true);
        projectGrid.setCanRemoveRecords(true);
        projectGrid.setAutoFetchData(false);
        projectGrid.setPreventDuplicates(true);

        ListGridField employeeIdField2 = new ListGridField("employeeId");
        employeeIdField2.setWidth("20%");

        ListGridField employeeNameField2 = new ListGridField("employeeName");
        employeeNameField2.setWidth("40%");
        ListGridField projectCodeField2 = new ListGridField("projectCode");

        projectGrid.setFields(employeeIdField2, employeeNameField2, projectCodeField2);

        HStack hStack = new HStack(10);
        hStack.setHeight(160);

        VStack vStack = new VStack();
        LayoutSpacer spacer = new LayoutSpacer();
        spacer.setHeight(30);
        vStack.addMember(spacer);
        vStack.addMember(employeesGrid);

        hStack.addMember(vStack);

        Img arrowImg = new Img("icons/32/arrow_right.png", 32, 32);
        arrowImg.setLayoutAlign(Alignment.CENTER);
        arrowImg.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                projectGrid.transferSelectedData(employeesGrid);
            }
        });
        hStack.addMember(arrowImg);

        VStack vStack2 = new VStack();

        final DynamicForm projectSelectorForm = new DynamicForm();
        projectSelectorForm.setWidth(300);
        projectSelectorForm.setHeight(30);

        SelectItem selectItem = new SelectItem("projectCode", "Team for Project");
        selectItem.setDefaultValue(projects[0]);
        selectItem.setValueMap(projects);
        selectItem.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                Criteria criteria = projectSelectorForm.getValuesAsCriteria();
                projectGrid.fetchData(criteria);
            }
        });
        projectSelectorForm.setFields(selectItem);

        vStack2.addMember(projectSelectorForm);
        vStack2.addMember(projectGrid);

        hStack.addMember(vStack2);

        projectGrid.fetchData(projectSelectorForm.getValuesAsCriteria());
        employeesGrid.fetchData();
        
        hStack.draw();
    }

}
</textarea>
<script class='javascript'>
dp.SyntaxHighlighter.HighlightAll("code");
</script>
</body>
</html>
